//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MarketModelPathwiseInverseFloater.h"
using namespace Cephei::QL::Models::Marketmodels::Products::Pathwise;
#include <gen/QL/Models/Marketmodels/EvolutionDescription.h>
#include <gen/QL/Models/Marketmodels/CurveState.h>
#include <gen/QL/Models/Marketmodels/MarketModelPathwiseMultiProduct.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ fixedAccruals, Cephei::Core::IVector<Double>^ floatingAccruals, Cephei::Core::IVector<Double>^ fixedStrikes, Cephei::Core::IVector<Double>^ fixedMultipliers, Cephei::Core::IVector<Double>^ floatingSpreads, Cephei::Core::IVector<Double>^ paymentTimes, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payer) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
    try
    {
#ifdef HANDLE
        _phMarketModelPathwiseInverseFloater = NULL;
#endif
        rateTimes->Lock();
        INativeVector<Double>^ _NCIrateTimes = rateTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCrateTimes = safe_cast<CDoubleVector^>(_NCIrateTimes);
        std::vector<QuantLib::Time>& _rateTimes = static_cast<std::vector<QuantLib::Time>&> (_NCrateTimes->GetReference ());
        fixedAccruals->Lock();
        INativeVector<Double>^ _NCIfixedAccruals = fixedAccruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedAccruals = safe_cast<CDoubleVector^>(_NCIfixedAccruals);
        std::vector<QuantLib::Real>& _fixedAccruals = static_cast<std::vector<QuantLib::Real>&> (_NCfixedAccruals->GetReference ());
        floatingAccruals->Lock();
        INativeVector<Double>^ _NCIfloatingAccruals = floatingAccruals->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloatingAccruals = safe_cast<CDoubleVector^>(_NCIfloatingAccruals);
        std::vector<QuantLib::Real>& _floatingAccruals = static_cast<std::vector<QuantLib::Real>&> (_NCfloatingAccruals->GetReference ());
        fixedStrikes->Lock();
        INativeVector<Double>^ _NCIfixedStrikes = fixedStrikes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedStrikes = safe_cast<CDoubleVector^>(_NCIfixedStrikes);
        std::vector<QuantLib::Real>& _fixedStrikes = static_cast<std::vector<QuantLib::Real>&> (_NCfixedStrikes->GetReference ());
        fixedMultipliers->Lock();
        INativeVector<Double>^ _NCIfixedMultipliers = fixedMultipliers->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfixedMultipliers = safe_cast<CDoubleVector^>(_NCIfixedMultipliers);
        std::vector<QuantLib::Real>& _fixedMultipliers = static_cast<std::vector<QuantLib::Real>&> (_NCfixedMultipliers->GetReference ());
        floatingSpreads->Lock();
        INativeVector<Double>^ _NCIfloatingSpreads = floatingSpreads->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCfloatingSpreads = safe_cast<CDoubleVector^>(_NCIfloatingSpreads);
        std::vector<QuantLib::Real>& _floatingSpreads = static_cast<std::vector<QuantLib::Real>&> (_NCfloatingSpreads->GetReference ());
        paymentTimes->Lock();
        INativeVector<Double>^ _NCIpaymentTimes = paymentTimes->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCpaymentTimes = safe_cast<CDoubleVector^>(_NCIpaymentTimes);
        std::vector<QuantLib::Time>& _paymentTimes = static_cast<std::vector<QuantLib::Time>&> (_NCpaymentTimes->GetReference ());
        bool _payer = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (payer) ? (bool)ValueHelper::Convert (payer->Value) : true); //4
        _ppMarketModelPathwiseInverseFloater = new boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater> (new QuantLib::MarketModelPathwiseInverseFloater ( _rateTimes,  _fixedAccruals,  _floatingAccruals,  _fixedStrikes,  _fixedMultipliers,  _floatingSpreads,  _paymentTimes,  _payer ));
        SetMarketModelPathwiseMultiProduct (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (rateTimes != nullptr) rateTimes->Unlock();    //not optional
        if (fixedAccruals != nullptr) fixedAccruals->Unlock();    //not optional
        if (floatingAccruals != nullptr) floatingAccruals->Unlock();    //not optional
        if (fixedStrikes != nullptr) fixedStrikes->Unlock();    //not optional
        if (fixedMultipliers != nullptr) fixedMultipliers->Unlock();    //not optional
        if (floatingSpreads != nullptr) floatingSpreads->Unlock();    //not optional
        if (paymentTimes != nullptr) paymentTimes->Unlock();    //not optional
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater>& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseInverseFloater = NULL;
#endif
	_ppMarketModelPathwiseInverseFloater = &childNative;
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (QuantLib::MarketModelPathwiseInverseFloater& childNative, Object^ owner) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseInverseFloater = NULL;
#endif
	_ppMarketModelPathwiseInverseFloater = new boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater> (&childNative);
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
    _MarketModelPathwiseInverseFloaterOwner = owner;
    _MarketModelPathwiseMultiProductOwner = owner;
}

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (CMarketModelPathwiseInverseFloater^ copy) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseInverseFloater = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMarketModelPathwiseInverseFloater = new boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater> (copy->GetShared());
        _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (PLATFORM::Type^ t) : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseInverseFloater = NULL;
#endif
	if (!t->IsSubclassOf(CMarketModelPathwiseInverseFloater::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (QuantLib::Handle<QuantLib::MarketModelPathwiseInverseFloater>& childNative, Object^ owner)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
	_phMarketModelPathwiseInverseFloater = &childNative;
	_ppMarketModelPathwiseInverseFloater = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
    _MarketModelPathwiseInverseFloaterOwner = owner;
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (QuantLib::Handle<QuantLib::MarketModelPathwiseInverseFloater> childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
	_phMarketModelPathwiseInverseFloater = &childNative;
	_ppMarketModelPathwiseInverseFloater = &static_cast<boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater>>(childNative.currentLink());
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::CMarketModelPathwiseInverseFloater (QuantLib::MarketModelPathwiseInverseFloater childNative)  : CMarketModelPathwiseMultiProduct(CMarketModelPathwiseInverseFloater::typeid)
{
#ifdef HANDLE
	_phMarketModelPathwiseInverseFloater = NULL;
#endif
	_ppMarketModelPathwiseInverseFloater = new boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater> (new QuantLib::MarketModelPathwiseInverseFloater (childNative));
    _ppMarketModelPathwiseMultiProduct = new boost::shared_ptr<QuantLib::MarketModelPathwiseMultiProduct> (boost::dynamic_pointer_cast<QuantLib::MarketModelPathwiseMultiProduct> (*_ppMarketModelPathwiseInverseFloater));
}
#endif

Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::~CMarketModelPathwiseInverseFloater ()
{
    if (_ppMarketModelPathwiseInverseFloater != NULL)
    {
	    delete _ppMarketModelPathwiseInverseFloater;
        _ppMarketModelPathwiseInverseFloater = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::!CMarketModelPathwiseInverseFloater ()
{
    if (_ppMarketModelPathwiseInverseFloater != NULL)
    {
	    delete _ppMarketModelPathwiseInverseFloater;
    }
}
QuantLib::MarketModelPathwiseInverseFloater& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::GetReference ()
{
    if (_ppMarketModelPathwiseInverseFloater == NULL) throw REFNEW NativeNullException ();
	return **_ppMarketModelPathwiseInverseFloater;
}
boost::shared_ptr<QuantLib::MarketModelPathwiseInverseFloater>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::GetShared ()
{
    if (_ppMarketModelPathwiseInverseFloater == NULL) throw REFNEW NativeNullException ();
	return *_ppMarketModelPathwiseInverseFloater;
}
QuantLib::MarketModelPathwiseInverseFloater* Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::GetPointer ()
{
    if (_ppMarketModelPathwiseInverseFloater == NULL) throw REFNEW NativeNullException ();
	return &**_ppMarketModelPathwiseInverseFloater;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MarketModelPathwiseInverseFloater>& Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::GetHandle ()
{
	if (_phMarketModelPathwiseInverseFloater == NULL)
	{
		_phMarketModelPathwiseInverseFloater = new Handle<QuantLib::MarketModelPathwiseInverseFloater> (*_ppMarketModelPathwiseInverseFloater);
	}
	return *_phMarketModelPathwiseInverseFloater;
}
#endif
bool Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::HasNative () 
{
	return (_ppMarketModelPathwiseInverseFloater != NULL);
}

Boolean Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::AlreadyDeflated::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppMarketModelPathwiseInverseFloater)->alreadyDeflated ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::IEvolutionDescription^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::Evolution::get ()
{
    try
    {
    	QuantLib::EvolutionDescription& _rv = (QuantLib::EvolutionDescription&)(*_ppMarketModelPathwiseInverseFloater)->evolution ( );   
        Cephei::QL::Models::Marketmodels::CEvolutionDescription^ _nrv = REFNEW Cephei::QL::Models::Marketmodels::CEvolutionDescription (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::MaxNumberOfCashFlowsPerProductPerStep::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseInverseFloater)->maxNumberOfCashFlowsPerProductPerStep ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::NumberOfProducts::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMarketModelPathwiseInverseFloater)->numberOfProducts ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<Double>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::PossibleCashFlowTimes::get ()
{
    try
    {
    	std::vector<QuantLib::Time> _rv = (std::vector<QuantLib::Time>)(*_ppMarketModelPathwiseInverseFloater)->possibleCashFlowTimes ( );   
        Cephei::Core::IVector<Double>^ _nrv = REFNEW CoVector<Double> (REFNEW CDoubleVector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseInverseFloater^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::Reset::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	(*_ppMarketModelPathwiseInverseFloater)->reset ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::Core::IVector<UInt64>^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater::SuggestedNumeraires::get ()
{
    try
    {
    	std::vector<QuantLib::Size> _rv = (std::vector<QuantLib::Size>)(*_ppMarketModelPathwiseInverseFloater)->suggestedNumeraires ( );   
        Cephei::Core::IVector<UInt64>^ _nrv = REFNEW CoVector<UInt64> (REFNEW CUInt64Vector (_rv));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Models::Marketmodels::Products::Pathwise::IMarketModelPathwiseInverseFloater^ Cephei::QL::Models::Marketmodels::Products::Pathwise::CMarketModelPathwiseInverseFloater_Factory::Create (Cephei::Core::IVector<Double>^ rateTimes, Cephei::Core::IVector<Double>^ fixedAccruals, Cephei::Core::IVector<Double>^ floatingAccruals, Cephei::Core::IVector<Double>^ fixedStrikes, Cephei::Core::IVector<Double>^ fixedMultipliers, Cephei::Core::IVector<Double>^ floatingSpreads, Cephei::Core::IVector<Double>^ paymentTimes, Microsoft::FSharp::Core::FSharpOption<Boolean>^ payer)
{
    return REFNEW CMarketModelPathwiseInverseFloater ( rateTimes,  fixedAccruals,  floatingAccruals,  fixedStrikes,  fixedMultipliers,  floatingSpreads,  paymentTimes,  payer);
}
